package net.ib.mtalk.network.task;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Common.FileKey;
import net.ib.mntalk.protocol.Common.FileToken;
import net.ib.mntalk.protocol.Common.UserMessage;
import net.ib.mntalk.protocol.Session.AcquireFileTokenReq;
import net.ib.mntalk.protocol.Session.AcquireFileTokenRes;
import net.ib.mtalk.network.MethodId.Session;
import net.ib.mtalk.network.TcpEnvelope;
import android.content.Context;
import android.os.Handler;

/**
 * COMMENT : 파일전송을 담당하는 TASK<br>
 *   아직 전송Flow와 Protocol이 정해지지 않았다!!!!<br>
 * PRE-EXECUTE : 파일을 전송하려면 FileToken을  받아야 하므로 TcpEnvelope을 만든다<br>
 * POST-EXECUTE : FileToken을 받으면 SendMessageTask를 만들어서 실제 전송을 보낸다<br>
 * @author eugenel
 *
 */
public class SendFileTask extends AsyncNetTask {
	
	private UserMessage mUserMessage;

	public SendFileTask(Context _context, Handler _uiHandler, UserMessage _userMessage) {
		super(_context, _uiHandler);
		mUserMessage = _userMessage;
	}

	@Override
	protected TcpEnvelope onPreExecute() {
		AcquireFileTokenReq tokenReq = new AcquireFileTokenReq();
		
		TcpEnvelope rev = new TcpEnvelope.Builder()
			.setType(TcpEnvelope.REQUEST)
			.setMessage(tokenReq)
			.setMethodId(Session.File.AcquireFileToken)
			.build();
		return rev;
	}

	@Override
	protected void onPostExecute(int resultCode, Object requestObj,
			Object resultObj) {
		if(resultObj==null){
			return;
		}
		
		AcquireFileTokenRes res = (AcquireFileTokenRes) resultObj;
		if(res.getResponseCode()==Common.SUCCESS){
			FileToken fileToken = res.getFileToken();
			FileKey fileKey = new FileKey();
			mUserMessage.setAttachFile(fileKey);
			
			// 다음 Task 호출
//			SendMessageReq sendMessageReq = new SendMessageReq();
//			sendMessageReq.setMessage(mUserMessage);
//			SendMessageTask sendMessageTask = new SendMessageTask(mContext, mUiHandler, sendMessageReq);
//			sendMessageTask.execute();
		}// End Of If(success)
	}

}
